# -*- coding: utf-8 -*-
"""
Created on Mon Nov 19 19:53:10 2018

@author: 陈忠涛
"""

import pandas as pd
import numpy as np

# 用户的信息
uinfo = ['user id', 'age', 'gender', 'occupation', 'zip code']
# 读取用户信息
users =pd.read_table('ml-100k/u.user', sep = '\|', names = uinfo, engine='python')
# 评分记录
cinfo = ['user id', 'item id', 'rating', 'timestamp']
# 读取评分信息
cdatas = pd.read_table('ml-100k/u.data', sep='\t', names = cinfo, engine='python')

users_df = pd.DataFrame()
users_df['user id'] = users['user id']
users_df['gender'] = users['gender']

cdatas_df = pd.DataFrame()
cdatas_df['user id'] = cdatas['user id']
cdatas_df['rating'] = cdatas['rating']

# 合并
data_df = pd.merge(users_df, cdatas_df)
# 透视表函数
gender_table = pd.pivot_table(data_df, index = ['gender', 'user id'], values = 'rating')

gender_df = pd.DataFrame(gender_table)
Female_df = gender_df.query("gender == ['F']")
Male_df = gender_df.query("gender == ['M']")

Female_std = np.std(Female_df)
Male_std = np.std(Male_df)
print ('性别', '\n女\t%.6f' % Female_std, '\n男\t%.6f' % Male_std)